package com.ebupt.migu.music.media.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ebupt.migu.mediadata.pojo.*;
import com.ebupt.migu.music.common.entity.ResultObject;
import com.ebupt.migu.music.common.util.EmptyUtils;
import com.ebupt.migu.music.media.dto.*;
import com.ebupt.migu.music.media.entity.*;
import com.ebupt.migu.music.media.enums.ConditionEnum;
import com.ebupt.migu.music.media.enums.FropertyEnum;
import com.ebupt.migu.music.media.enums.IdentifierEnum;
import com.ebupt.migu.music.media.enums.OperatorEnum;
import com.ebupt.migu.music.media.mapper.*;
import com.ebupt.migu.music.media.service.MediaService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;


@Service
@Slf4j
public class MediaServiceImpl implements MediaService {


    @Autowired
    MediaMapper mediaMapper;

    @Autowired
    MediaActivityMapper mediaActivityMapper;

    @Autowired
    MediaBannerMapper bannerMapper;

    @Autowired
    MediaBlockMapper mediaBlockMapper;

    @Autowired
    MediaKingMapper mediaKingMapper;

    @Autowired
    MediaRulesMapper mediaRulesMapper;

    @Autowired
    MediaCheckMapper mediaCheckMapper;

    @Override
    public ResultObject getConditions() {

        ResultObject resultObject = new ResultObject();
        List<Property> properties = mediaMapper.selectPropertyList();
        Map<Integer, List<Property>> PropertyListMap = properties.stream().collect(Collectors.groupingBy(Property::getPropertyCode));
        ArrayList<HashMap> list = new ArrayList<>();

        //城市
        List<Map<String, Object>> cs = mediaMapper.selectCS();

        //省份
        List<HashMap<Object, Object>> sf = cs.stream().map(m -> {
                    HashMap<Object, Object> hashMap = new HashMap<>();
                    hashMap.put("adCode", m.get("cityCode").toString().substring(0, 2));
                    hashMap.put("provinceName", m.get("provinceName"));
                    return hashMap;
                }).filter(distinctByKey(e -> e.get("adCode")))
                .collect(Collectors.toList());

        for (Map.Entry<Integer, List<Property>> listEntry : PropertyListMap.entrySet()) {
            FropertyEnum fropertyEnum = FropertyEnum.get(listEntry.getKey());
            HashMap resultMap = new HashMap<>();
            resultMap.put("PropertyCode", listEntry.getKey());
            resultMap.put("PropertyName", fropertyEnum.getName());
            List<HashMap> categorys = listEntry.getValue().stream().map(property -> {
                ConditionEnum conditionEnum = ConditionEnum.get(property.getPropertyCode(), property.getConditionKey());
                HashMap<String, Object> map = new HashMap<>(3);
                map.put("ConditionCode", conditionEnum.getConditionKey());
                map.put("ConditionValue", conditionEnum.getValue());
                map.put("ConditionName", conditionEnum.getName());
                map.put("ConditionTip", property.getConditionTip());

                if (property.getPropertyCode() == FropertyEnum.FROPERTY_ENUM_PROVINCE.getCode()) {
                    //省份
                    if (property.getConditionKey() == ConditionEnum.adCode.getConditionKey()) {
                        map.put("datas", sf);
                    }

                    //城市
                    if (property.getConditionKey() == ConditionEnum.cityCode.getConditionKey()) {
                        map.put("datas", cs);
                    }


                } else {
                    List<OperatorEnum> conditionDataList = OperatorEnum.getByType(property.getDataType());
                    if (conditionDataList != null) {
                        List<HashMap> datas = conditionDataList.stream().map(cd -> {
                            HashMap cdmap = new HashMap<>();
                            cdmap.put("DataKey", cd.getCode());
                            cdmap.put("DataValue", cd.getName());
                            return cdmap;
                        }).collect(Collectors.toList());
                        map.put("datas", datas);
                    }

                }

                if (property.getRelations() != null) {
                    List<HashMap> relations = Arrays.stream(property.getRelations().split(",")).map(s -> {
                        HashMap imap = new HashMap<>();
                        imap.put("Relation", s);
                        imap.put("RelationValue", IdentifierEnum.getNameByCode(Integer.parseInt(s)));
                        return imap;
                    }).collect(Collectors.toList());
                    map.put("relations", relations);
                }
                return map;
            }).collect(Collectors.toList());

            resultMap.put("categorys", categorys);
            list.add(resultMap);
        }
        resultObject.setCode("0000");
        resultObject.setMsg("成功");
        resultObject.setData(list);
        return resultObject;
    }

    public static <T> Predicate<T> distinctByKey(Function<? super T, Object> keyExtractor) {
        Map<Object, Boolean> seen = new ConcurrentHashMap<>();
        return object -> seen.putIfAbsent(keyExtractor.apply(object), Boolean.TRUE) == null;
    }

    @Override
    public ResultObject list(PageDto pageDto) {
        ResultObject resultObject = new ResultObject<>();
        Page<MediaActivity> page = new Page<>();
        BeanUtils.copyProperties(pageDto, page);
        QueryWrapper<MediaActivity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .like(!EmptyUtils.isEmpty(pageDto.getActivityName()), MediaActivity::getActivityName, pageDto.getActivityName())
                .eq(!EmptyUtils.isEmpty(pageDto.getExecuteType()), MediaActivity::getExecuteType, pageDto.getExecuteType())
                .eq(!EmptyUtils.isEmpty(pageDto.getExecuteStatus()), MediaActivity::getExecuteStatus, pageDto.getExecuteStatus())
                .eq(!EmptyUtils.isEmpty(pageDto.getCreatorName()), MediaActivity::getCreatorName, pageDto.getCreatorName())
                .eq(!EmptyUtils.isEmpty(pageDto.getCreatorDepartment()), MediaActivity::getCreatorDepartment, pageDto.getCreatorDepartment())
                .between(!EmptyUtils.isEmpty(pageDto.getSTime()) && !EmptyUtils.isEmpty(pageDto.getETime()), MediaActivity::getCreateTime, pageDto.getSTime(), pageDto.getETime())
                .orderByDesc(MediaActivity::getCreateTime);
        Page<MediaActivity> mediaActivityPage = mediaActivityMapper.selectPage(page, queryWrapper);
        resultObject.setCode("0000");
        resultObject.setMsg("成功");
        resultObject.setData(mediaActivityPage);
        return resultObject;

    }

    @Override
    public ResultObject checkList(PageDto pageDto) {

        ResultObject resultObject = new ResultObject<>();
        Page<MediaCheck> page = new Page<>();
        BeanUtils.copyProperties(pageDto, page);
        QueryWrapper<MediaCheck> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(!EmptyUtils.isEmpty(pageDto.getActivityId()), MediaCheck::getActivityId, pageDto.getActivityId())
                .like(!EmptyUtils.isEmpty(pageDto.getActivityName()), MediaCheck::getActivityName, pageDto.getActivityName())
                .eq(!EmptyUtils.isEmpty(pageDto.getState()), MediaCheck::getState, pageDto.getState())
                .eq(!EmptyUtils.isEmpty(pageDto.getCreatorName()), MediaCheck::getProposer, pageDto.getCreatorName())
                .eq(!EmptyUtils.isEmpty(pageDto.getCreatorDepartment()), MediaCheck::getDepartment, pageDto.getCreatorDepartment())
                .between(!EmptyUtils.isEmpty(pageDto.getSTime()) && !EmptyUtils.isEmpty(pageDto.getETime()), MediaCheck::getCreateTime, pageDto.getSTime(), pageDto.getETime())
                .orderByDesc(MediaCheck::getCreateTime);
        Page<MediaCheck> mediaActivityPage = mediaCheckMapper.selectPage(page, queryWrapper);
        resultObject.setCode("0000");
        resultObject.setMsg("成功");
        resultObject.setData(mediaActivityPage);
        return resultObject;

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultObject add(AddDto addDto) {
        ResultObject resultObject = new ResultObject<>();
        //创建基础数据
        MediaActivity mediaActivity = new MediaActivity();
        BeanUtils.copyProperties(addDto, mediaActivity);
        mediaActivity.setCreateTime(DateUtil.now());
        Integer integer = Math.toIntExact(mediaActivityMapper.selectCount(new QueryWrapper<MediaActivity>().lambda()
                .eq(MediaActivity::getDisplayType, mediaActivity.getDisplayType())
                .eq(MediaActivity::getVisitPoint, mediaActivity.getVisitPoint())
        ));
        mediaActivity.setPriority(integer + 1);
        if (mediaActivity.getExecuteType() == 1) {
            mediaActivity.setExecuteStatus(1);
        } else {
            mediaActivity.setExecuteStatus(3);
        }
        mediaActivityMapper.insert(mediaActivity);
        //判断是组件还是banner * 展示类型：1-组件；2-banner；
        if (addDto.getDisplayType().equals(1)) {
            // * 访问位置：1-侧边栏主页；2-听歌-金刚区；
            if (addDto.getVisitPoint().equals(1)) {
                List<BlockDto> blockDtoList = addDto.getBlockDtoList();
                blockDtoList.forEach(blockDto -> {
                    MediaBlock mediaBlock = new MediaBlock();
                    BeanUtils.copyProperties(blockDto, mediaBlock);
                    mediaBlock.setActivityId(addDto.getActivityId());
                    mediaBlockMapper.insert(mediaBlock);
                    List<MediaRules> mediaRules = blockDto.getMediaRules();
                    mediaRules.forEach(m -> {
                        m.setActivityId(addDto.getActivityId());
                        m.setRulesId(blockDto.getRulesId());
                        mediaRulesMapper.insert(m);
                    });

                });
            }
            // * 访问位置：1-侧边栏主页；2-听歌-金刚区；
            if (addDto.getVisitPoint().equals(2)) {
                List<KingDto> kingDtoList = addDto.getKingDtoList();
                kingDtoList.forEach(kingDto -> {
                    MediaKing mediaKing = new MediaKing();
                    BeanUtils.copyProperties(kingDto, mediaKing);
                    mediaKing.setActivityId(addDto.getActivityId());
                    mediaKingMapper.insert(mediaKing);
                    List<MediaRules> mediaRules = kingDto.getMediaRules();
                    mediaRules.forEach(m -> {
                        m.setActivityId(addDto.getActivityId());
                        m.setRulesId(kingDto.getRulesId());
                        mediaRulesMapper.insert(m);
                    });
                });

            }
        }

        if (addDto.getDisplayType().equals(2)) {
            if (addDto.getVisitPoint().equals(1)) {
                List<BannerDto> bannerDtoList = addDto.getBannerDtoList();
                bannerDtoList.forEach(bannerDto -> {
                    MediaBanner mediaBanner = new MediaBanner();
                    BeanUtils.copyProperties(bannerDto, mediaBanner);
                    mediaBanner.setActivityId(addDto.getActivityId());
                    bannerMapper.insert(mediaBanner);
                    List<MediaRules> mediaRules = bannerDto.getMediaRules();
                    mediaRules.forEach(m -> {
                        m.setActivityId(addDto.getActivityId());
                        m.setRulesId(bannerDto.getRulesId());
                        mediaRulesMapper.insert(m);
                    });
                });
            }
        }
        if (addDto.getExecuteType().equals(1)) {
            //创建审核
            MediaCheck mediaCheck = new MediaCheck();
            mediaCheck.setApproverName(mediaActivity.getApproverName());
            mediaCheck.setActivityId(mediaActivity.getActivityId());
            mediaCheck.setActivityName(mediaActivity.getActivityName());
            mediaCheck.setCreateTime(mediaActivity.getCreateTime());
            mediaCheck.setState(0);
            mediaCheck.setProposer(mediaActivity.getCreatorName());
            mediaCheck.setDepartment(mediaActivity.getCreatorDepartment());
            mediaCheckMapper.insert(mediaCheck);
        }
        resultObject.setCode("0000");
        resultObject.setMsg("成功");
        return resultObject;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultObject update(AddDto addDto) {
        ResultObject resultObject = new ResultObject<>();
        //创建基础数据
        MediaActivity mediaActivity = new MediaActivity();
        BeanUtils.copyProperties(addDto, mediaActivity);

        QueryWrapper<MediaCheck> mediaCheckQueryWrapper = new QueryWrapper<>();
        mediaCheckQueryWrapper.lambda()
                .eq(true, MediaCheck::getActivityId, addDto.getActivityId());
        mediaCheckMapper.delete(mediaCheckQueryWrapper);

        if (mediaActivity.getExecuteType() == 1) {
            mediaActivity.setExecuteStatus(1);
            //创建审核
            MediaCheck mediaCheck = new MediaCheck();
            mediaCheck.setApproverName(mediaActivity.getApproverName());
            mediaCheck.setActivityId(mediaActivity.getActivityId());
            mediaCheck.setActivityName(mediaActivity.getActivityName());
            mediaCheck.setCreateTime(DateUtil.now());
            mediaCheck.setState(0);
            mediaCheck.setProposer(mediaActivity.getCreatorName());
            mediaCheck.setDepartment(mediaActivity.getCreatorDepartment());
            mediaCheckMapper.insert(mediaCheck);
        } else {
            mediaActivity.setExecuteStatus(3);
        }
        UpdateWrapper<MediaActivity> mediaActivityUpdateWrapper = new UpdateWrapper<>();
        mediaActivityUpdateWrapper.lambda()
                .eq(true, MediaActivity::getActivityId, addDto.getActivityId());
        mediaActivityMapper.update(mediaActivity, mediaActivityUpdateWrapper);


        //判断是组件还是banner * 展示类型：1-组件；2-banner；
        if (addDto.getDisplayType().equals(1)) {
            // * 访问位置：1-侧边栏主页；2-听歌-金刚区；
            if (addDto.getVisitPoint().equals(1)) {
                List<BlockDto> blockDtoList = addDto.getBlockDtoList();
                QueryWrapper<MediaBlock> blockQueryWrapper = new QueryWrapper<>();
                blockQueryWrapper.lambda()
                        .eq(true, MediaBlock::getActivityId, addDto.getActivityId());
                QueryWrapper<MediaRules> queryWrapper = new QueryWrapper<>();
                queryWrapper.lambda()
                        .eq(true, MediaRules::getActivityId, addDto.getActivityId());
                mediaRulesMapper.delete(queryWrapper);
                mediaBlockMapper.delete(blockQueryWrapper);
                blockDtoList.forEach(blockDto -> {
                    MediaBlock mediaBlock = new MediaBlock();
                    BeanUtils.copyProperties(blockDto, mediaBlock);
                    mediaBlock.setActivityId(addDto.getActivityId());
                    mediaBlockMapper.insert(mediaBlock);
                    List<MediaRules> mediaRules = blockDto.getMediaRules();
                    mediaRules.forEach(m -> {
                        m.setActivityId(addDto.getActivityId());
                        m.setRulesId(blockDto.getRulesId());
                        mediaRulesMapper.insert(m);
                    });

//                    UpdateWrapper<MediaBlock> blockDtoUpdateWrapper = new UpdateWrapper<>();
//                    blockDtoUpdateWrapper.lambda()
//                            .eq(true,MediaBlock::getActivityId,addDto.getActivityId())
//                            .eq(true,MediaBlock::getBlockId,blockDto.getBlockId());
//                    mediaBlockMapper.update(mediaBlock,blockDtoUpdateWrapper);
//                    List<MediaRules> mediaRules = blockDto.getMediaRules();
//                    mediaRules.forEach(m -> {
//                        UpdateWrapper<MediaRules> mediaRulesUpdateWrapper = new UpdateWrapper<>();
//                        mediaRulesUpdateWrapper.lambda()
//                                .eq(true,MediaRules::getActivityId,addDto.getActivityId())
//                                .eq(true,MediaRules::getRulesId,blockDto.getRulesId());
//                        mediaRulesMapper.update(m,mediaRulesUpdateWrapper);
//                    });

                });
            }


            // * 访问位置：1-侧边栏主页；2-听歌-金刚区；
            if (addDto.getVisitPoint().equals(2)) {
                List<KingDto> kingDtoList = addDto.getKingDtoList();
                QueryWrapper<MediaKing> kingQueryWrapper = new QueryWrapper<>();
                kingQueryWrapper.lambda()
                        .eq(true, MediaKing::getActivityId, addDto.getActivityId());
                QueryWrapper<MediaRules> queryWrapper = new QueryWrapper<>();
                queryWrapper.lambda()
                        .eq(true, MediaRules::getActivityId, addDto.getActivityId());
                mediaRulesMapper.delete(queryWrapper);
                mediaKingMapper.delete(kingQueryWrapper);
                kingDtoList.forEach(kingDto -> {
                    MediaKing mediaKing = new MediaKing();
                    BeanUtils.copyProperties(kingDto, mediaKing);

                    mediaKing.setActivityId(addDto.getActivityId());
                    mediaKingMapper.insert(mediaKing);
                    List<MediaRules> mediaRules = kingDto.getMediaRules();
                    mediaRules.forEach(m -> {
                        m.setActivityId(addDto.getActivityId());
                        m.setRulesId(kingDto.getRulesId());
                        mediaRulesMapper.insert(m);
                    });

//                    UpdateWrapper<MediaKing> kingUpdateWrapper = new UpdateWrapper<>();
//                    kingUpdateWrapper.lambda()
//                            .eq(true,MediaKing::getActivityId,addDto.getActivityId())
//                            .eq(true,MediaKing::getRulesId,mediaKing.getRulesId());
//                    mediaKingMapper.update(mediaKing,kingUpdateWrapper);
//                    List<MediaRules> mediaRules = kingDto.getMediaRules();
//                    mediaRules.forEach(m -> {
//                        UpdateWrapper<MediaRules> mediaRulesUpdateWrapper = new UpdateWrapper<>();
//                        mediaRulesUpdateWrapper.lambda()
//                                .eq(true,MediaRules::getActivityId,addDto.getActivityId())
//                                .eq(true,MediaRules::getRulesId,kingDto.getRulesId());
//                        mediaRulesMapper.update(m,mediaRulesUpdateWrapper);
//                    });
                });
            }
        }

        if (addDto.getDisplayType().equals(2)) {
            if (addDto.getVisitPoint().equals(1)) {
                QueryWrapper<MediaBanner> bannerQueryWrapper = new QueryWrapper<>();
                bannerQueryWrapper.lambda()
                        .eq(true, MediaBanner::getActivityId, addDto.getActivityId());
                QueryWrapper<MediaRules> queryWrapper = new QueryWrapper<>();
                queryWrapper.lambda()
                        .eq(true, MediaRules::getActivityId, addDto.getActivityId());
                mediaRulesMapper.delete(queryWrapper);
                bannerMapper.delete(bannerQueryWrapper);
                List<BannerDto> bannerDtoList = addDto.getBannerDtoList();
                bannerDtoList.forEach(bannerDto -> {
                    MediaBanner mediaBanner = new MediaBanner();
                    BeanUtils.copyProperties(bannerDto, mediaBanner);
                    mediaBanner.setActivityId(addDto.getActivityId());
                    bannerMapper.insert(mediaBanner);
                    List<MediaRules> mediaRules = bannerDto.getMediaRules();
                    mediaRules.forEach(m -> {
                        m.setActivityId(addDto.getActivityId());
                        m.setRulesId(bannerDto.getRulesId());
                        mediaRulesMapper.insert(m);
                    });
//                    UpdateWrapper<MediaBanner> bannerUpdateWrapper = new UpdateWrapper<>();
//                    bannerUpdateWrapper.lambda()
//                            .eq(true,MediaBanner::getActivityId,addDto.getActivityId())
//                            .eq(true,MediaBanner::getRulesId,mediaBanner.getRulesId());
//                    bannerMapper.update(mediaBanner,bannerUpdateWrapper);
//                    List<MediaRules> mediaRules = bannerDto.getMediaRules();
//                    mediaRules.forEach(m -> {
//                        UpdateWrapper<MediaRules> mediaRulesUpdateWrapper = new UpdateWrapper<>();
//                        mediaRulesUpdateWrapper.lambda()
//                                .eq(true,MediaRules::getActivityId,addDto.getActivityId())
//                                .eq(true,MediaRules::getRulesId,bannerDto.getRulesId());
//                        mediaRulesMapper.update(m,mediaRulesUpdateWrapper);
//                    });
                });
            }
        }


        resultObject.setCode("0000");
        resultObject.setMsg("成功");
        return resultObject;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultObject updateStatus(Map<String, Object> params) {
        ResultObject resultObject = new ResultObject<>();
        UpdateWrapper<MediaActivity> mediaActivityUpdateWrapper = new UpdateWrapper<>();
        mediaActivityUpdateWrapper.lambda()
                .eq(true, MediaActivity::getActivityId, params.get("activityId"))
                .set(true, MediaActivity::getExecuteStatus, params.get("executeStatus"));
        mediaActivityMapper.update(null, mediaActivityUpdateWrapper);
        if (!params.get("executeStatus").equals(1)) {
            QueryWrapper<MediaCheck> mediaCheckQueryWrapper = new QueryWrapper<>();
            mediaCheckQueryWrapper.lambda()
                    .eq(true, MediaCheck::getActivityId, params.get("activityId"));
            mediaCheckMapper.delete(mediaCheckQueryWrapper);
        }
        resultObject.setCode("0000");
        resultObject.setMsg("成功");
        return resultObject;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultObject checkUpdate(Map<String, Object> params) {
        ResultObject resultObject = new ResultObject<>();
        String s = params.get("state").toString();
        UpdateWrapper<MediaCheck> mediaCheckUpdateWrapper = new UpdateWrapper<>();
        mediaCheckUpdateWrapper.lambda()
                .eq(true, MediaCheck::getActivityId, params.get("activityId"))
                .set(true, MediaCheck::getState, s)
                .set(true, MediaCheck::getUpdateTime, new Date())
                .set(true, MediaCheck::getRemark, params.get("remark"));
        mediaCheckMapper.update(null, mediaCheckUpdateWrapper);
        UpdateWrapper<MediaActivity> mediaActivityUpdateWrapper = new UpdateWrapper<>();

        mediaActivityUpdateWrapper.lambda()
                .eq(true, MediaActivity::getActivityId, params.get("activityId"));

        if (s.equals("1")) {
            mediaActivityUpdateWrapper.lambda().set(true, MediaActivity::getExecuteStatus, 3);
        }

        if (s.equals("2")) {
            mediaActivityUpdateWrapper.lambda()
                    .set(true, MediaActivity::getExecuteStatus, 2);
        }
        mediaActivityMapper.update(null, mediaActivityUpdateWrapper);
        resultObject.setCode("0000");
        resultObject.setMsg("成功");
        return resultObject;
    }

    @Override
    public ResultObject detail(Map<String, Object> params) {
        ResultObject resultObject = new ResultObject<>();
        String activityId = params.get("activityId").toString();
        MediaActivity mediaActivity = mediaActivityMapper.selectOne(new QueryWrapper<MediaActivity>().lambda().eq(true, MediaActivity::getActivityId, activityId));
        resultObject.setCode("0000");
        resultObject.setMsg("成功");
        resultObject.setData(mediaActivity);
        return resultObject;
    }

    @Override
    public ResultObject checkDetail(Map<String, Object> params) {
        ResultObject resultObject = new ResultObject<>();
        String activityId = params.get("activityId").toString();
        MediaCheck mediaCheck = mediaCheckMapper.selectOne(new QueryWrapper<MediaCheck>().lambda().eq(true, MediaCheck::getActivityId, activityId));
        resultObject.setCode("0000");
        resultObject.setMsg("成功");
        resultObject.setData(mediaCheck);
        return resultObject;
    }

    @Override
    public ResultObject effectList(Map<String, Object> params) {
        log.info("请求参数:{}", params);
        int current = Integer.valueOf(String.valueOf(params.get("current")));
        int every = Integer.valueOf(String.valueOf(params.get("every")));
        params.put("pageStart", (current - 1) * every);
        params.put("rows", every);
        ResultObject resultObject = new ResultObject<>();
        List<Map<String, Object>> list = mediaMapper.selectEffectList(params);
        Integer count = mediaMapper.count(params);
        // 分页
        com.ebupt.migu.music.common.entity.Page<Map<String, Object>> page = new com.ebupt.migu.music.common.entity.Page<>();
        page.setTotal(count);
        page.setData(list);
        resultObject.setData(page);
        return resultObject;
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultObject delete(Map<String, Object> params) {
        ResultObject resultObject = new ResultObject<>();
        String activityId = params.get("activityId").toString();
        MediaActivity mediaActivity = mediaActivityMapper.selectOne(new QueryWrapper<MediaActivity>().lambda().eq(MediaActivity::getActivityId, activityId));
        mediaActivityMapper.delete(new QueryWrapper<MediaActivity>().lambda().eq(true, MediaActivity::getActivityId, activityId));
        bannerMapper.delete(new QueryWrapper<MediaBanner>().lambda().eq(true, MediaBanner::getActivityId, activityId));
        mediaBlockMapper.delete(new QueryWrapper<MediaBlock>().lambda().eq(true, MediaBlock::getActivityId, activityId));
        mediaRulesMapper.delete(new QueryWrapper<MediaRules>().lambda().eq(true, MediaRules::getActivityId, activityId));
        mediaKingMapper.delete(new QueryWrapper<MediaKing>().lambda().eq(true, MediaKing::getActivityId, activityId));
        mediaCheckMapper.delete(new QueryWrapper<MediaCheck>().lambda().eq(true, MediaCheck::getActivityId, activityId));
        //删除数据，需要重排活动优先级
        Map<String, Object> map = new HashMap<>();
        map.put("type", "3");
        map.put("priority", mediaActivity.getPriority());
        map.put("displayType", mediaActivity.getDisplayType());
        map.put("visitPoint", mediaActivity.getVisitPoint());
        priority(map);
        resultObject.setCode("0000");
        resultObject.setMsg("成功");
        return resultObject;

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultObject priority(Map<String, Object> params) {
        ResultObject resultObject = new ResultObject<>();
        List<MediaActivity> activityList = mediaActivityMapper.selectList(new QueryWrapper<MediaActivity>().lambda()
                .eq(MediaActivity::getDisplayType, params.get("displayType"))
                .eq(MediaActivity::getVisitPoint, params.get("visitPoint"))
        );
        //按照优先级排序
        activityList.sort(Comparator.comparing(MediaActivity::getPriority));
        int cur = Integer.parseInt(params.get("priority").toString());
        int tmp;
        //升序情况，交换当前位置前一个数据的优先级
        if ("1".equals(params.get("type"))) {
            if (cur == 1) {
                resultObject.setCode("-1");
                resultObject.setMsg("已是最高优先级");
                return resultObject;
            }
            for (int i = 0; i < activityList.size(); i++) {
                if (cur == activityList.get(i).getPriority()) {
                    tmp = cur;
                    activityList.get(i).setPriority(activityList.get(i - 1).getPriority());
                    activityList.get(i - 1).setPriority(tmp);
                }
            }
        }
        //降序情况，交换当前位置后一个数据的优先级
        if ("2".equals(params.get("type"))) {
            if (cur >= activityList.size()) {
                resultObject.setCode("-1");
                resultObject.setMsg("已是最低优先级");
                return resultObject;
            }
            for (int i = 0; i < activityList.size() - 1; i++) {
                if (cur == activityList.get(i).getPriority()) {
                    tmp = cur;
                    activityList.get(i).setPriority(activityList.get(i + 1).getPriority());
                    activityList.get(i + 1).setPriority(tmp);
                }
            }
        }
        //重置排序
        if ("3".equals(params.get("type"))) {
            for (int i = 0; i < activityList.size(); i++) {
                activityList.get(i).setPriority(i + 1);
            }
        }
        activityList.forEach(e -> mediaActivityMapper.updateById(e));
        resultObject.setCode("0000");
        resultObject.setMsg("成功");
        return resultObject;
    }


}
